package com.nullbrain.sp;

/*
 * This file is part of SlashPlayed. SlashPlayed is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. SlashPlayed is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details. You should have received a copy of the GNU General Public License
 * along with SlashPlayed. If not, see <http://www.gnu.org/licenses/>.
 */
import java.util.logging.Logger;

import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.config.Configuration;

public class SlashPlayed extends JavaPlugin {

    // Field for configuration
    public Configuration config;

    // Logger for debug purposes
    Logger log = Logger.getLogger("Minecraft");

    // descriptor for plugin
    public static PluginDescriptionFile pdf;

    // Player Listner
    public final SPPlayerListener playerlistener = new SPPlayerListener(this);

    @Override
    public void onEnable() {
        pdf = this.getDescription();
        this.log.info(pdf.getFullName() + " version " + pdf.getVersion() + "- by " + pdf.getAuthors()
                + " has been activated.");

        setupConfig();

        if (config.getBoolean("sp.data.floodcontrol.useFloodControl", false))
            getServer().getScheduler().scheduleSyncRepeatingTask(this, new FloodControlTask(this), 1200, 1200);

        PluginManager pm = getServer().getPluginManager();

        this.getCommand("sp").setExecutor(new SPCommandHandler(this));

        pm.registerEvent(Event.Type.PLAYER_JOIN, playerlistener, Priority.Lowest, this);
        pm.registerEvent(Event.Type.PLAYER_QUIT, playerlistener, Priority.Normal, this);
    }

    @Override
    public void onDisable() {
        this.getServer().getScheduler().cancelTasks(this);

    }

    public void setupConfig() {
        // TODO Write code

        // Setup main config field
        this.config = this.getConfiguration();

        if (this.config.getKeys().isEmpty()) {
            this.log.info("Building clean copy of config file");

         
            this.config.setProperty("sp.data.storage.method", "txt");  //txt| not implemented -> sql|sqlite
           
            //db config(sql)
            this.config.setProperty("sp.data.storage.sql.host", "host");
            this.config.setProperty("sp.data.storage.sql.username", "user"); 
            this.config.setProperty("sp.data.storage.sql.password", "pass");
            this.config.setProperty("sp.data.storage.sql.database", "db"); 
            this.config.setProperty("sp.data.storage.sql.table", "table"); 

            //if using sqlite
            this.config.setProperty("sp.data.storage.sqlite.dbfile", "path to *.db file here");  
            
            // Flood control
            this.config.setProperty("sp.data.floodcontrol.useFloodControl", "false");
            this.config.setProperty("sp.data.floodcontrol.uses", "5");                 
            this.config.setProperty("sp.data.floodcontrol.time", "60000");
                                                                         

            this.config.setProperty("sp.error.tooFewParam", "There were not enough parameters supplied.");                                                                                                                                                                                                    
            this.config.setProperty("sp.error.tooManyParam", "There were too many parameters specified.");
            this.config.setProperty("sp.error.tooManyCommands", "You have met the alloted amount of uses per set time frame.");

            // save to default file
            this.config.save();
        }

    }

    public void setupPerms() {
        // TODO Nothing

        // Not going to be implemented unless requested. There are only two commands a
        // player can use and there is option for flood control. If a player feel the
        // need to abuse this feature, and flood control they deserve to be banned.
    }

}
